package Day2025_03_14;

public class LeetCode1567 {
    public int getMaxLen(int[] nums) {
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        int len = 0;
        if (nums[0] > 0) {
            f[0] = 1; g[0] = 0; len = 1;
        } else if (nums[0] < 0) {
            f[0] = 0; g[0] = 1;
        } else {
            f[0] = g[0] = 0;
        }
        for (int i = 1; i < n; i++) {
            if (nums[i] > 0) {
                f[i] = f[i - 1] + 1;
                g[i] = g[i - 1] == 0 ? 0 : g[i - 1] + 1;
            } else if (nums[i] < 0) {
                f[i] = g[i - 1] == 0 ? 0 : g[i - 1] + 1;
                g[i] = f[i - 1] + 1;
            } else {
                f[i] = g[i] = 0;
            }
            len = Math.max(len, f[i]);
        }
        return len;
    }
}
